//
//  TabBarController.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/14.
//

#import "TabBarController.h"




@interface TabBarController ()<UITabBarDelegate,UITabBarControllerDelegate>

@end

@implementation TabBarController

+ (void)initialize {
    
    UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
    
    NSMutableDictionary *dictNormal = [NSMutableDictionary dictionary];
    dictNormal[NSForegroundColorAttributeName] = RDRGB(0xCBC7C7);
    dictNormal[NSFontAttributeName] = Bold(10);
    
    NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
    dictSelected[NSForegroundColorAttributeName] = RDRGB(0x714933);
    dictSelected[NSFontAttributeName] = Bold(10);
    
    [tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:dictSelected forState:UIControlStateSelected];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate  = self;
    self.tabBar.backgroundColor = UIColor.whiteColor;
    [self setSelectedIndex:0];
    [self setChildViewControllers];
        
}

-(void)setChildViewControllers{
    
    
    id homeVC = [[NSClassFromString(@"HomeVC") alloc]init];
    [self addChildViewControllerWithViewController:homeVC
                                             title:@"监测"
                                             image:@"TabBar_home_no"
                                     selectedImage:@"TabBar_home_sel"
                                               tag:1000];
    
    id gameVC = [[NSClassFromString(@"SleepVC") alloc]init];
    [self addChildViewControllerWithViewController:gameVC
                                             title:@"入寐"
                                             image:@"TabBar_sleep_no"
                                     selectedImage:@"TabBar_sleep_sel"
                                               tag:1001];
    
    id marketVC = [[NSClassFromString(@"EnergyVC") alloc]init];
    [self addChildViewControllerWithViewController:marketVC
                                             title:@"能量"
                                             image:@"TabBar_enerhy_no"
                                     selectedImage:@"TabBar_enerhy_sel"
                                               tag:1002];
    
    id meVC = [[NSClassFromString(@"MeVC") alloc]init];
    [self addChildViewControllerWithViewController:meVC
                                             title:@"我的"
                                             image:@"TabBar_me_no"
                                     selectedImage:@"TabBar_me_sel"
                                               tag:1003];
}

-(void)addChildViewControllerWithViewController:(UIViewController *)childController title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage tag:(NSInteger )tag
{
    BaseNavController *nav = [[BaseNavController alloc] initWithRootViewController:childController];
    UIImage *myImage = [UIImage imageNamed:image];
    childController.tabBarItem.image = myImage;
    
    UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
    childController.tabBarItem.selectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    nav.tabBarItem.title  = title;
    nav.tabBarItem.tag = tag;
    if (IS_PhoneXAll) {
            nav.tabBarItem.imageInsets = UIEdgeInsetsMake(2, 0, -2, 0);
    }
    
    if(@available(iOS 13.0, *)) {
       self.tabBar.tintColor= RDRGB(0x000000);
       self.tabBar.unselectedItemTintColor = RDRGB(0x999999);
      }
    [self addChildViewController:nav];
}

//这个是UITabBarController的代理方法
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

//    if (!ISLOGIN) {
//        if (viewController.tabBarItem.tag == 1003 || viewController.tabBarItem.tag == 1001) {
//            [(AppDelegate*)[[UIApplication     sharedApplication] delegate] gotoLogin];
//            return NO;
//        }else{
//            return YES;
//        }
//    }else{
        
    
    NSInteger selectedIndex = [tabBarController.viewControllers indexOfObject:viewController];
    if (selectedIndex == 1) {
        [UIView animateWithDuration:0.5 animations:^{
            self.tabBar.backgroundColor = RDRGB(0x8D7E6C);
        }];
    }
    else{
        [UIView animateWithDuration:0.5 animations:^{
            self.tabBar.backgroundColor = UIColor.whiteColor;
        }];
    }
        
        return YES;
//    }
}



@end
